﻿using MediatR;
using T_Person.BaseRep.Interface;
using T_Person.Read.APi.Applaction.Command.TotalPersonnel;
using T_Person.Read.APi.Dto;

namespace T_Person.Read.APi.Applaction.CommandHandlers.TotalPersonnelHandlers
{
    public class TotalPersonnelSearhDepartmentHandlers : IRequestHandler<TotalPersonnelBydepartmentCommand, List<PersonnelAgeGroup>>
    {
        private readonly IEmployeeRep employeeRep;

        public TotalPersonnelSearhDepartmentHandlers(IEmployeeRep employeeRep)
        {
            this.employeeRep = employeeRep;
        }

        public async Task<List<PersonnelAgeGroup>> Handle(TotalPersonnelBydepartmentCommand request, CancellationToken cancellationToken)
        {
            var list = await employeeRep.GetvaluesByResult(x => !x.IsDel && x.EmployeeStyle > 0 && x.EmployeeStyle < 5);
            int listcount = list.Count();
            var rnamelist = list.GroupBy(x => x.DepartId).Select(x => new PersonnelAgeGroup
            {
                name = x.Key,
                number = x.Count()
            }).ToList();
            return rnamelist;
        }
    }
}
